@Singleton public class MyClientBean { @Inject InitBallot<MyClientBean> ballot; @PostConstruct public void doStuff() { // ... do some work ... ballot.voteForInit(); } }
A problem commonly associated with building large applications in the browser is ensuring that things happen in the proper order when code starts executing. Errai IOC provides you tools which permit you to ensure things happen before initialization, and forcing things to happen after initialization of all of the Errai services.
In order to prevent initialization of the the bus and it's services so that you can do necessary configuration, especially if you are writing extensions to the Errai framework itself, you can create an implicit startup dependency on your bean by injecting an org.jboss.errai.ioc.client.api.InitBallot<?>.
@Singleton public class MyClientBean { @Inject InitBallot<MyClientBean> ballot; @PostConstruct public void doStuff() { // ... do some work ... ballot.voteForInit(); } }
Sending RPC calls to the server from inside constructors and @PostConstruct methods in Errai is not always reliable due to the fact that the bus and RPC proxies initialize asynchronously with the rest of the application. Therefore it is often desirable to have such things happen in a post-initialization task, which is exposed in the ClientMessageBus API. However, it is much cleaner to use the @AfterInitialization annotation on one of your bean methods.
@Singleton public class MyClientBean { @AfterInitialization public void doStuffAfterInit() { // ... do some work ... } }